package com.chinasofti.java8.demo02;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestLambda {

/*
Consumer: 消费型接口
Supplier： 供给型接口
Function：函数型接口
Predicate：断言型接口
 */

    @Test
    public void test4(){
        List<String> list = Arrays.asList("hello","lalalalala","java","lambda","ok");
        List<String> list1 = filterStr(list, (s) -> s.length() > 3);
        System.out.println("list1 = " + list1);
    }

    //满足条件的字符串放入集合
    public List<String> filterStr(List<String> list, Predicate<String> pre){
        List<String> strList = new ArrayList<>();
        for (String str:list){
            if(pre.test(str)){
                strList.add(str);
            }
        }
        return strList;
    }
    @Test
    public void test3(){
        String s = strHandler("helloworld", (str) -> str.substring(2, 5));
        System.out.println("s = " + s);
    }

    //处理字符串
    public String strHandler(String str,Function<String,String> fun){
        return fun.apply(str);
    }
    @Test
    public void test2(){
        List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
        System.out.println(numList);
    }
    //生产指定个数的整数，放入集合中
    public List<Integer> getNumList(int num, Supplier<Integer> sup){
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<num;i++){
            Integer n = sup.get();
            list.add(n);
        }
        return list;
    }
    @Test
    public void test1(){
        happy(10000,(m)->System.out.print("阿啦啦啦拉阿拉啦啦啦啦，每次消费"+m+"元"));
    }
    public void happy(double money , Consumer<Double> con) {
        con.accept(money);
    }

}